package com.dt76.smallloanbackendservice.service.etao.impl;

import com.dt76.smallloanbackendservice.mapper.etao.RoleMapper;
import com.dt76.smallloanbackendservice.pojo.etao.Role;
import com.dt76.smallloanbackendservice.service.etao.RoleService;
import com.dt76.smallloanbackendservice.utils.DateUtils;
import com.dt76.smallloanbackendservice.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Page getAllRole(Integer pageIndex) {
        Page page = new Page();
        page.setTotalCount(roleMapper.getTotalCount());
        page.setPageIndex(pageIndex);
        List<Role> allRole = roleMapper.getAllRole((page.getPageIndex() - 1) * page.getPageSize(), page.getPageSize());
        for (Role role : allRole) {
            role.setCreateTimeStr(DateUtils.dateToString(role.getCreateTime(), "yyyy-MM-dd"));
        }
        page.setRows(allRole);
        return page;
    }

    @Override
    public Role getRoleById(Integer rId) {
        return roleMapper.getRoleById(rId);
    }

    @Override
    public Boolean addRole(Role role) {
        boolean fg = false;
        if (roleMapper.saveRole(role) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public Boolean delRole(Integer rId) {
        boolean fg = false;
        if (roleMapper.delRole(rId) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public Boolean updRole(Role role) {
        boolean fg = false;
        if (roleMapper.updateRole(role) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public List<Role> getRoleBydId(Integer dId) {
        return roleMapper.getRoleBydId(dId);
    }
}
